package com.ksy.common.image;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ksy.common.R;

import java.util.ArrayList;

/**
 * Created by XXX.
 */
public class ImageShowView extends RelativeLayout {


    private ImageViewPager imageViewPager;
    private TextView indexTextView;
    private ImageSrcClickListener srcClickListener;

    public void addImageList(ArrayList<String> allImages, int index) {
        imageViewPager.addImageList(allImages);
        ArrayList<String> imageList = imageViewPager.getImageList();
        if (imageList.isEmpty()) {
            indexTextView.setText("0");
        } else {
            if (index == 0) {
                indexTextView.setText((1) + "/" + imageList.size());
            }
        }
        imageViewPager.setCurrentItem(index, true);
    }

    public void setImageSrcClickListener(ImageSrcClickListener srcClickListener) {
        this.srcClickListener = srcClickListener;
    }


    public ImageShowView(Context context) {
        this(context, null);
    }

    public ImageShowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundResource(R.color.common_colorTextDark);
        int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.0f, context.getResources().getDisplayMetrics());
        imageViewPager = new ImageViewPager(context);
        imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indexTextView.setText((position + 1) + "/" + imageViewPager.getImageList().size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        imageViewPager.setImageSrcClickListener(new ImageSrcClickListener() {
            @Override
            public void onImageSrcClick(String url, int position) {
                if (srcClickListener != null) {
                    srcClickListener.onImageSrcClick(url, position);
                }
            }
        });
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(imageViewPager, params);
        indexTextView = new TextView(context);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);
        layoutParams.addRule(CENTER_HORIZONTAL, TRUE);
        layoutParams.bottomMargin = dp1 * 15;
        indexTextView.setTextColor(Color.WHITE);
        addView(indexTextView, layoutParams);
    }

    private class ImageViewPager extends com.ksy.common.view.ViewPager {
        private ImageSrcClickListener srcClickListener;

        public void setImageSrcClickListener(ImageSrcClickListener srcClickListener) {
            this.srcClickListener = srcClickListener;
        }

        private ImageAdapter imageAdapter;

        public ImageViewPager(Context context) {
            this(context, null);
        }

        public ImageViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
            imageAdapter = new ImageAdapter();
            imageAdapter.setImageSrcClickListener(new ImageSrcClickListener() {
                @Override
                public void onImageSrcClick(String url, int position) {
                    if (srcClickListener != null) {
                        srcClickListener.onImageSrcClick(url, position);
                    }
                }
            });
            setAdapter(imageAdapter);
        }


        public void addImageList(ArrayList<String> allImages) {
            imageAdapter.addImageList(allImages);
        }

        public ArrayList<String> getImageList() {
            return imageAdapter.getImageList();
        }
    }

    public interface ImageSrcClickListener {
        void onImageSrcClick(String url, int position);
    }

    private class ImageAdapter extends PagerAdapter {
        private ArrayList<XImageView> caches = new ArrayList<>();
        private ArrayList<String> allImages = new ArrayList<>();
        private ImageSrcClickListener srcClickListener;

        public void setImageSrcClickListener(ImageSrcClickListener srcClickListener) {
            this.srcClickListener = srcClickListener;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            XImageView imageView;
            final String url = allImages.get(position);
            if (!caches.isEmpty()) {
                imageView = caches.remove(0);
            } else {
                imageView = new XImageView(container.getContext());
                imageView.setRealViewOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (srcClickListener != null) {
                            srcClickListener.onImageSrcClick(url, position);
                        }
                    }
                });
                ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
                imageView.setLayoutParams(layoutParams);
            }
            imageView.load(url);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
            caches.add((XImageView) object);
        }

        @Override
        public int getCount() {
            return allImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        public void addImageList(ArrayList<String> allImages) {
            this.allImages.addAll(allImages);
            notifyDataSetChanged();
        }

        public ArrayList<String> getImageList() {
            return allImages;
        }
    }
}
